/* 
 * This is an object that let's us get and then perform certain functions on
 * an array of frames, belonging to a user-specific run
 */

/*
 * Ctor, initializes variables that will later be used by the functions
 */
var Frames = function(user_id, run_id) {
    this.def_frames = getFrames(user_id, run_id);
    this.frames = [];
    this.pulse = [];
    this.altitude = [];
    this.speed = [];
    this.coords = [];  
    this.ticks = [];
    this.start_time = 0;
    this.total_time = 0;
    this.maxPulse = {coord: [], pulse: 0};
    this.maxAltitude = {coord: [], alt: 0};
    this.maxSpeed = {coord: [], speed: 0};
};

Frames.prototype = (function() {
    return {
        /*
         * Initialize the object with values from the frames of
         * a user under a specific run, to provide the other functions
         * with data. A deferred object is returned to ensure that
         * we've downloaded all the frame-data.
         */
        initialize: function() {
            me_frames = this;
            def_promise = $.Deferred(function(deferred) {

                me_frames.def_frames.done(function(frames) {
                    me_frames.frames = frames;
                    me_frames.start_time = me_frames.frames[0].time_label;
                    me_frames.total_time = me_frames.frames[frames.length - 1].time_label - me_frames.frames.start_time;
                    for (var i = 0; i < me_frames.frames.length; i++) {
                        var dist = me_frames.frames[i].distance;
                        var pul = me_frames.frames[i].pulse;
                        var spd = me_frames.frames[i].speed;
                        var alt = me_frames.frames[i].alt;
                        var crd = [me_frames.frames[i].lat, me_frames.frames[i].lon];
                        if(pul > me_frames.maxPulse.pulse) {
                            me_frames.maxPulse.coord = crd;
                            me_frames.maxPulse.pulse = pul;
                        }
                        if(alt > me_frames.maxAltitude.alt) {
                            me_frames.maxAltitude.coord = crd;
                            me_frames.maxAltitude.alt = alt;
                        }
                        if(spd > me_frames.maxSpeed.speed) {
                            me_frames.maxSpeed.coord = crd;
                            me_frames.maxSpeed.speed = spd;
                        }
                        me_frames.pulse.push([dist, pul]);
                        me_frames.speed.push([dist, spd]);
                        me_frames.altitude.push([dist, alt]);
                        me_frames.coords.push(crd);                       
                    }
                    console.log("Maxalt: " + me_frames.maxAltitude.alt.toString())

                    for (var i = 0; i < me_frames.frames[me_frames.frames.length - 1].distance; i += 1000) {
                        me_frames.ticks.push(i);
                    }
                    me_frames.ticks.push(me_frames.frames[me_frames.frames.length - 1].distance);
                    deferred.resolve();
                });
            }).promise();
            return def_promise;
        },
        getFrames: function() {
            return this.frames;
        },
        getPulses: function() {
            return this.pulse;
        },
        getAltitudes: function() {
            return this.altitude;
        },
        getSpeeds: function() {
            return this.speed;
        },
        getCoords: function() {
            return this.coords;
        },
        getTicks: function() {
            return this.ticks;
        },
        getStartTime: function() {
            return this.start_time;
        },
        getTotalTime: function() {
            return this.total_time;
        },
        getMaxPulse: function() {
            return this.maxPulse;
        },
        getMaxAltitude: function() {
            return this.maxAltitude;
        },
        getMaxSpeed: function() {
            return this.maxSpeed;
        }
    };
}());